package com.bai.translation;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class ResourceFileGenerator {

    public static void generateResourceFiles(Map<String, String> md5Map, String zhOutputPath, String enOutputPath, Map<String, String> translations) throws IOException {
        try (BufferedWriter zhWriter = new BufferedWriter(new FileWriter(zhOutputPath, true));
             BufferedWriter enWriter = new BufferedWriter(new FileWriter(enOutputPath, true))) {
            for (Map.Entry<String, String> entry : md5Map.entrySet()) {
                String key = entry.getKey();
                String chinese = entry.getValue();
                String english = translations.getOrDefault(chinese, chinese);
                zhWriter.write("i18n."+key + "=" + chinese);
                zhWriter.newLine();
                enWriter.write("i18n."+key + "=" + english);
                enWriter.newLine();
            }
        }
    }

    public static void updateResourceFiles(String enResourceFilePath, String zhResourceFilePath, Map<String, String> excelMap) {
        // 1. 加载已有的资源文件
        LinkedHashMap<String, String> enResourceMap = loadResourceFile(enResourceFilePath);
        LinkedHashMap<String, String> zhResourceMap = loadResourceFile(zhResourceFilePath);

        // 2. 加载Excel中的中文-英文对

        // 3. 遍历Excel数据，更新enResourceMap中的英文翻译
        for (Map.Entry<String, String> entry : excelMap.entrySet()) {
            String chineseText = entry.getKey();
            String excelEnglishText = entry.getValue();

            // 通过中文的MD5计算key
            String md5Key = "i18n."+MD5Util.md5(chineseText);

            // 检查资源文件中是否存在该中文翻译
            if (zhResourceMap.containsKey(md5Key)) {
                String resourceChineseText = zhResourceMap.get(md5Key);

                // 如果中文相同但英文不同，则更新英文
                if (chineseText.equals(resourceChineseText)) {
                    String resourceEnglishText = enResourceMap.get(md5Key);
                    if (!excelEnglishText.equals(resourceEnglishText)) {
                        System.out.println("Updating English for key: " + md5Key);
                        enResourceMap.put(md5Key, excelEnglishText);  // 更新英文
                    }
                }
            }
        }

        // 4. 保存更新后的资源文件
        saveResourceFile(enResourceFilePath, enResourceMap);
        //5. excelMap合并zhResourceFilePath和enResourceFilePath原已配置的翻译
        for (Map.Entry<String, String> entry : zhResourceMap.entrySet()) {
            String md5Key = entry.getKey();
            String chineseText = entry.getValue();
            String englishText = enResourceMap.getOrDefault(md5Key, chineseText);
            excelMap.put(chineseText, englishText);
        }
    }

    // 保存资源文件
    public static void saveResourceFile(String resourceFilePath, LinkedHashMap<String, String> resourceMap) {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(resourceFilePath))) {
            for (Map.Entry<String, String> entry : resourceMap.entrySet()) {
                writer.write(entry.getKey() + "=" + entry.getValue());
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 读取资源文件，返回Map<中文MD5, 对应的翻译>
    public static LinkedHashMap<String, String> loadResourceFile(String resourceFilePath) {
        //后续中英文资源文件替换，防止顺序改变，使用LinkedHashMap
        LinkedHashMap<String, String> resourceMap = new LinkedHashMap<>();
        // 逐行读取文件，假设每行格式为 "MD5_KEY=翻译"
        try (BufferedReader reader = new BufferedReader(new FileReader(resourceFilePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("=");
                if (parts.length == 2) {
                    resourceMap.put(parts[0], parts[1]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resourceMap;
    }
}





